package com.lyf.boot.modules.system.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.lyf.boot.constant.CacheConstant;
import com.lyf.boot.core.model.DictCache;
import com.lyf.boot.core.service.IDictService;
import com.lyf.boot.modules.system.dao.SysDictDao;
import com.lyf.boot.modules.system.entity.vo.SysDictVo;
import com.lyf.boot.modules.system.service.ISysDictService;
import com.lyf.boot.utils.RedisUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Luyufan
 * @date 2023/7/7 16:53
 */
@Service
@RequiredArgsConstructor
public class SysDictServiceImpl implements ISysDictService, IDictService {

    private final SysDictDao sysDictDao;


    @Override
    public List<SysDictVo> getSysDictListByType(String dictType) {
        return sysDictDao.selectSysDictByDictType(dictType);
    }

    @Override
    public Map<String, String> getSysDictMapByType(String dictType) {
        Map<String, String> resultMap = new HashMap<>(0);
        List<SysDictVo> list = getSysDictListByType(dictType);
        if (CollUtil.isNotEmpty(list)){
            resultMap = list.parallelStream().collect(Collectors.toMap(SysDictVo::getDictKey, SysDictVo::getDictValue));
        }
        return resultMap;
    }

    @Override
    public DictCache getSysDict(String dictType, String dictKey) {
        String dictRedisKey = CacheConstant.DICT_CACHE + dictType;
        Object cacheObj = RedisUtil.getCacheMapValue(dictRedisKey, dictKey);
        if(cacheObj == null){
            return null;
        }
        return (DictCache) cacheObj;
    }

    @Override
    public String getSysDictValue(String dictType, String dictKey) {
        DictCache dictCache = getSysDict(dictType, dictKey);
        return null == dictCache ? null : dictCache.getDictValue();
    }

    /**
     * 根据字典类型和字典值获取字典标签
     *
     * @param dictType  字典类型
     * @param dictKey 字典值
     * @param separator 分隔符
     * @return 字典标签
     */
    @Override
    public String getDictValue(String dictType, String dictKey, String separator) {
        StringBuilder propertyString = new StringBuilder();
        List<SysDictVo> dataList = getSysDictListByType(dictType);

        if (StrUtil.containsAny(dictKey, separator) && CollUtil.isNotEmpty(dataList)) {
            for (SysDictVo dict : dataList) {
                for (String value : dictKey.split(separator)) {
                    if (value.equals(dict.getDictKey())) {
                        propertyString.append(dict.getDictValue() + separator);
                        break;
                    }
                }
            }
        } else {
            for (SysDictVo dict : dataList) {
                if (dictKey.equals(dict.getDictKey())) {
                    return dict.getDictValue();
                }
            }
        }
        return StrUtil.removeSuffix(propertyString.toString(), separator);
    }

    /**
     * 根据字典类型和字典标签获取字典值
     *
     * @param dictType  字典类型
     * @param dictKey 字典标签
     * @param separator 分隔符
     * @return 字典值
     */
    @Override
    public String getDictKey(String dictType, String dictKey, String separator) {
        StringBuilder propertyString = new StringBuilder();
        List<SysDictVo> dataList = getSysDictListByType(dictType);

        if (StrUtil.containsAny(dictKey, separator) && CollUtil.isNotEmpty(dataList)) {
            for (SysDictVo dict : dataList) {
                for (String label : dictKey.split(separator)) {
                    if (label.equals(dict.getDictValue())) {
                        propertyString.append(dict.getDictKey() + separator);
                        break;
                    }
                }
            }
        } else {
            for (SysDictVo dict : dataList) {
                if (dictKey.equals(dict.getDictValue())) {
                    return dict.getDictKey();
                }
            }
        }
        return StrUtil.removeSuffix(propertyString.toString(), separator);
    }
}
